package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IExpressService;
import com.mdd.admin.validate.ExpressCreateValidate;
import com.mdd.admin.validate.ExpressUpdateValidate;
import com.mdd.admin.validate.ExpressSearchValidate;
import com.mdd.admin.vo.ExpressListedVo;
import com.mdd.admin.vo.ExpressDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Express;
import com.mdd.common.mapper.ExpressMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 快点订单记录（物流）实现类
 * @author cxt
 */
@Service
public class ExpressServiceImpl implements IExpressService {
        
    @Resource
    ExpressMapper expressMapper;

    /**
     * 快点订单记录（物流）列表
     *
     * @author cxt
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ExpressListedVo>
     */
    @Override
    public PageResult<ExpressListedVo> list(PageValidate pageValidate, ExpressSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Express> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        expressMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userId@user_id:int",
            "=:expressId@express_id:int",
            "=:expressNo@express_no:str",
            "=:expressSource@express_source:int",
            "=:expressStatus@express_status:int",
            "=:payIs@pay_is:int",
            "=:payWay@pay_way:int",
            "=:payTime@pay_time:int",
            "=:transactionId@transaction_id:str",
            "=:address:str",
            "=:addressContact@address_contact:str",
            "=:addressMobile@address_mobile:str",
            "=:addressContent@address_content:str",
            "=:couponListId@coupon_list_id:int",
            "=:money:str",
            "=:goodsMoney@goods_money:str",
            "=:expressMoney@express_money:str",
            "=:couponMoney@coupon_money:str",
            "=:needPayMoney@need_pay_money:str",
            "=:payMoney@pay_money:str",
            "=:goodsNum@goods_num:int",
            "=:deliveryType@delivery_type:int",
            "=:expressIs@express_is:int",
            "=:expressTime@express_time:int",
            "=:confirmTime@confirm_time:int",
            "=:cancelTime@cancel_time:int",
            "=:afterDeadline@after_deadline:int",
            "=:userRemark@user_remark:str",
            "=:shopRemark@shop_remark:str",
        });

        IPage<Express> iPage = expressMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ExpressListedVo> list = new LinkedList<>();
        for(Express item : iPage.getRecords()) {
            ExpressListedVo vo = new ExpressListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 快点订单记录（物流）详情
     *
     * @author cxt
     * @param id 主键参数
     * @return Express
     */
    @Override
    public ExpressDetailVo detail(Integer id) {
        Express model = expressMapper.selectOne(
                new QueryWrapper<Express>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ExpressDetailVo vo = new ExpressDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 快点订单记录（物流）新增
     *
     * @author cxt
     * @param createValidate 参数
     */
    @Override
    public void add(ExpressCreateValidate createValidate) {
        Express model = new Express();
        model.setUserId(createValidate.getUserId());
        model.setExpressId(createValidate.getExpressId());
        model.setExpressNo(createValidate.getExpressNo());
        model.setExpressSource(createValidate.getExpressSource());
        model.setExpressStatus(createValidate.getExpressStatus());
        model.setPayIs(createValidate.getPayIs());
        model.setPayWay(createValidate.getPayWay());
        model.setPayTime(createValidate.getPayTime());
        model.setTransactionId(createValidate.getTransactionId());
        model.setAddress(createValidate.getAddress());
        model.setAddressContact(createValidate.getAddressContact());
        model.setAddressMobile(createValidate.getAddressMobile());
        model.setAddressContent(createValidate.getAddressContent());
        model.setCouponListId(createValidate.getCouponListId());
        model.setMoney(createValidate.getMoney());
        model.setGoodsMoney(createValidate.getGoodsMoney());
        model.setExpressMoney(createValidate.getExpressMoney());
        model.setCouponMoney(createValidate.getCouponMoney());
        model.setNeedPayMoney(createValidate.getNeedPayMoney());
        model.setPayMoney(createValidate.getPayMoney());
        model.setGoodsNum(createValidate.getGoodsNum());
        model.setDeliveryType(createValidate.getDeliveryType());
        model.setExpressIs(createValidate.getExpressIs());
        model.setExpressTime(createValidate.getExpressTime());
        model.setConfirmTime(createValidate.getConfirmTime());
        model.setCancelTime(createValidate.getCancelTime());
        model.setAfterDeadline(createValidate.getAfterDeadline());
        model.setUserRemark(createValidate.getUserRemark());
        model.setShopRemark(createValidate.getShopRemark());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        expressMapper.insert(model);
    }

    /**
     * 快点订单记录（物流）编辑
     *
     * @author cxt
     * @param updateValidate 参数
     */
    @Override
    public void edit(ExpressUpdateValidate updateValidate) {
        Express model = expressMapper.selectOne(
                new QueryWrapper<Express>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setExpressId(updateValidate.getExpressId());
        model.setExpressNo(updateValidate.getExpressNo());
        model.setExpressSource(updateValidate.getExpressSource());
        model.setExpressStatus(updateValidate.getExpressStatus());
        model.setPayIs(updateValidate.getPayIs());
        model.setPayWay(updateValidate.getPayWay());
        model.setPayTime(updateValidate.getPayTime());
        model.setTransactionId(updateValidate.getTransactionId());
        model.setAddress(updateValidate.getAddress());
        model.setAddressContact(updateValidate.getAddressContact());
        model.setAddressMobile(updateValidate.getAddressMobile());
        model.setAddressContent(updateValidate.getAddressContent());
        model.setCouponListId(updateValidate.getCouponListId());
        model.setMoney(updateValidate.getMoney());
        model.setGoodsMoney(updateValidate.getGoodsMoney());
        model.setExpressMoney(updateValidate.getExpressMoney());
        model.setCouponMoney(updateValidate.getCouponMoney());
        model.setNeedPayMoney(updateValidate.getNeedPayMoney());
        model.setPayMoney(updateValidate.getPayMoney());
        model.setGoodsNum(updateValidate.getGoodsNum());
        model.setDeliveryType(updateValidate.getDeliveryType());
        model.setExpressIs(updateValidate.getExpressIs());
        model.setExpressTime(updateValidate.getExpressTime());
        model.setConfirmTime(updateValidate.getConfirmTime());
        model.setCancelTime(updateValidate.getCancelTime());
        model.setAfterDeadline(updateValidate.getAfterDeadline());
        model.setUserRemark(updateValidate.getUserRemark());
        model.setShopRemark(updateValidate.getShopRemark());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        expressMapper.updateById(model);
    }

    /**
     * 快点订单记录（物流）删除
     *
     * @author cxt
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Express model = expressMapper.selectOne(
                new QueryWrapper<Express>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        expressMapper.updateById(model);
    }

}
